package by.dkusch.aircompany.webapp.page.base.panels;

import java.util.Locale;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;

public class LanguagePanel extends Panel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public LanguagePanel(String id) {
		super(id);

		Form<String> languageForm = new Form<String>("language-form"); // (1)

		languageForm.add(createLocaleChangingLink("ruButton", "RU"));
		languageForm.add(createLocaleChangingLink("enButton", "EN"));

		add(languageForm);
	}

	private Link<Void> createLocaleChangingLink(final String linkId, final String localeString) {
		return new Link<Void>(linkId) {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				changeUserLocaleTo(localeString); // (2)
				setResponsePage(getPage());
			}
		};
	}

	private void changeUserLocaleTo(String localeString) {
		getSession().setLocale(new Locale(localeString)); // (3)
	}

}
